
import turtle
import random
points=[[0,100],[0,125],[0,150]]
def tcs():
    turtle.clear()
    global food
    for i in range(len(points)):
        turtle.penup()
        turtle.goto(points[i])
        turtle.down()
        turtle.dot(20,'red')
    turtle.up()
    turtle.goto(food)
    turtle.down()
    turtle.dot(20,'cyan')
    new_point = points[-1].copy()
    if abs(new_point[0]-food[0])<20 and abs(new_point[1]-food[1])<20:
        food = [random.randint(-300, 300), random.randint(-300, 300)]
    else:
        points.pop(0)
    if new_point[0] > 320 or new_point[0] < -320 or new_point[1]>320or new_point[1]< -320:
        print('Game Over')
        turtle.clear()
        turtle.up()
        turtle.goto(0,0)
        turtle.down()
        turtle.pencolor('red')
        turtle.write('GAME OVER', True, 'center', ('Arial', 40, 'normal'))
    else:
        if c==1:
            new_point[1]+=25
        elif c==2:
            new_point[1]-=25
        elif c==3:
            new_point[0] += 25
        elif c==4:
            new_point[0] -= 25
        points.append(new_point)
        turtle.ontimer(tcs,400)
def a():
    global c
    c=1
def b():
    global c
    c=2
def d():
    global c
    c=3
def e():
    global c
    c=4
turtle.onkeypress(a,'Up')
turtle.onkeypress(b,'Down')
turtle.onkeypress(d,'Right')
turtle.onkeypress(e,'Left')
c=1
turtle.setup(640, 640)

turtle.listen()
turtle.tracer(False)
food = [random.randint(-300,300), random.randint(-300,300)]

tcs()

turtle.done()